package org.ww.edu.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.ww.commonutils.JwtUtils;
import org.ww.commonutils.R;
import org.ww.edu.bean.Comment;
import org.ww.edu.client.MemberClient;
import org.ww.edu.service.CommentService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author ww
 * @since 2020-11-10
 */
@RestController
@RequestMapping("/edu/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;

    /**
     * 分页查询评论信息
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/pageComment/{page}/{limit}/{courseId}")
    public R pageComment(@PathVariable Long page,@PathVariable Long limit,
                         @PathVariable String courseId){
        //将分页对象以map的形式将数据返回
        Map<String, Object> map = commentService.pageComment(page, limit,courseId);
        return R.ok().data(map);
    }

    /**
     * 添加评论信息
     * @param comment
     * @return
     */
    @PostMapping("/addComment")
    public R addComment(HttpServletRequest request,@RequestBody Comment comment){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)) {
            return R.error().code(28004).message("请登录");
        }
        commentService.addComment(memberId,comment);
        return R.ok();
    }

}

